package com.information.manage.basic.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.information.manage.basic.common.result.Result;
import com.information.manage.basic.common.token.TokenUtils;
import com.information.manage.basic.model.entity.SysUserDO;
import com.information.manage.basic.model.query.*;
import com.information.manage.basic.model.vo.FindUserVO;
import com.information.manage.basic.model.vo.LoginUserInfoVO;
import com.information.manage.basic.model.vo.UserMenuVO;
import com.information.manage.basic.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@Api(tags = "用户")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;


    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public Result login(@RequestBody LoginQuery loginQuery) {
        log.info("开始登录+-+——+——+——+——+——+——+——+——");
//        if(){
//            long expireTime = System.currentTimeMillis() + EXPIRE_DATE;
//            // 生成token
//            String token = TokenUtils.token(sysUserDO.getId(), sysUserDO.getName(), expireTime);
//            // 存储当前登录的用户
//            redisUtils.set(curLoginUserPre + id, token, expireTime, TimeUnit.MILLISECONDS);
//            return Result.ok(token);
//        }
        return sysUserService.login(loginQuery);
    }

    @GetMapping("/getLoginUser")
    @ApiOperation(value = "获取登录的用户信息", notes = "获取登录的用户信息")
    public Result<LoginUserInfoVO> getLoginUserInfo(HttpServletRequest request){
        Integer userId = TokenUtils.getUserId(StringUtils.isEmpty(request.getHeader("token")) ? request.getAttribute("token").toString() : request.getHeader("token"));
        if (userId == null) return Result.fail("未获取到用户信息");
        LoginUserInfoVO userInfo = sysUserService.getUserInfo(userId);
        return Result.ok(userInfo);
    }

    @GetMapping("/loginOut")
    public Result loginOut(HttpServletRequest request){
        Integer userId = TokenUtils.getUserId(StringUtils.isEmpty(request.getHeader("token")) ? request.getAttribute("token").toString() : request.getHeader("token"));
        sysUserService.loginOut(userId);
        return Result.ok();
    }

    @GetMapping("/find/{id}")
    @ApiOperation(value = "查询用户", notes = "查询用户")
    public Result<FindUserVO> findUserById(@PathVariable("id") int id){
        return Result.ok(sysUserService.findUserById(id));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    public Result<Integer> addUser(@RequestBody SysUserAddQuery sysUserAddQuery){
        return sysUserService.addUser(sysUserAddQuery);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    public Result<Integer> updateUser(@RequestBody SysUserUpdateQuery sysUserUpdateQuery){
        return sysUserService.updateUser(sysUserUpdateQuery);
    }

    @PostMapping("/bind/staff")
    @ApiOperation(value = "绑定员工", notes = "绑定员工")
    public Result<Boolean> userBindStaff(@RequestBody BindStaffQuery bindStaffQuery){
        return sysUserService.userBindStaff(bindStaffQuery.getStaffId(), bindStaffQuery.getUserId());
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    public Result<Boolean> deleteUser(@RequestBody List<Integer> userIds){
        return Result.ok(sysUserService.deleteUser(userIds));
    }

    @PutMapping("/move/menu")
    @ApiOperation(value = "用户移动菜单", notes = "用户移动菜单")
    public Result<Boolean> moveUserMenu(HttpServletRequest request, @RequestBody MoveUserMenuQuery moveUserMenuQuery){
//        int userId = request.getIntHeader("token");
        int userId = TokenUtils.getUserId(StringUtils.isEmpty(request.getHeader("token")) ? request.getAttribute("token").toString() : request.getHeader("token"));
        return Result.ok(sysUserService.moveUserMenu(userId, moveUserMenuQuery.getMenuId(), moveUserMenuQuery.isUp()));
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询用户分页", notes = "查询用户分页")
    public Result<Page<SysUserDO>> queryUserPage(int curPage,int pageSize){
        Page<SysUserDO> page=new Page<>(curPage,pageSize);
        Page<SysUserDO> userDOPage=sysUserService.page(page);
        return Result.ok(userDOPage);
    }
//    @ApiOperation(value = "查询用户分页", notes = "查询用户分页")
//    public Result<Page<SysUserPageVO>> queryUserPage(SysUserPageQuery sysUserPageQuery){
//        return sysUserService.queryUserPage(sysUserPageQuery);
//    }

    @GetMapping("/menu/tree")
    @ApiOperation(value = "查询用户的菜单树", notes = "查询用户的菜单树")
    public Result<List<UserMenuVO>> getUserMenu(HttpServletRequest request, UserMenuQuery userMenuQuery){
        return sysUserService.getUserMenu(request, userMenuQuery);
    }


}
